package com.zbkj.crmeb.block.model;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.exception.CrmebException;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Date;

@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
@TableName("eb_increment")
@ApiModel(value = "Increment对象", description = "增值表")
public class Increment implements Serializable {

    private static final long serialVersionUID = 1L;

    @ApiModelProperty(value = "增值ID")
    @TableId(value = "id", type = IdType.AUTO)
    private Integer id;
    @ApiModelProperty(value = "订单号")
    private String orderId;
    @ApiModelProperty(value = "商品价格")//售价
    private BigDecimal price;
    @ApiModelProperty(value = "成本价")
    private BigDecimal cost;
    @ApiModelProperty(value = "利润")
    private BigDecimal profit;
    @ApiModelProperty(value = "订单总返佣比例")
    private BigDecimal commissionPercent;
    @ApiModelProperty(value = "订单总佣金")
    private BigDecimal commission;
    @ApiModelProperty(value = "增值佣金比例")
    private BigDecimal incrementPercent;
    @ApiModelProperty(value = "增值佣金")//只用于增值
    private BigDecimal increment;
    @ApiModelProperty(value = "二级佣金比例")
    private BigDecimal increment2dPercent; //只是佣金，用于技术二级token commissionPercent 对应
    @ApiModelProperty(value = "二级佣金")
    private BigDecimal increment2d; //只是佣金，用于技术二级token 实际和commission 对应
    @ApiModelProperty(value = "二级佣金订单获得积分数量")
    private BigDecimal increment2dToken;
    @ApiModelProperty(value = "实际利润")
    private BigDecimal realProfit;
    @ApiModelProperty(value = "初始积分数量")
    private BigDecimal initToken;
    @ApiModelProperty(value = "订单获得积分数量")
    private BigDecimal incrementToken;
    @ApiModelProperty(value = "增值前积分数量")
    private BigDecimal beforeIncrementToken;
    @ApiModelProperty(value = "增值后积分数量")
    private BigDecimal afterIncrementToken;
    @ApiModelProperty(value = "增值前积分价值")
    private BigDecimal beforeIncrementPrice;
    @ApiModelProperty(value = "增值变化价值")
    private BigDecimal deltaIncrementPrice;
    @ApiModelProperty(value = "增值后积分价值")
    private BigDecimal afterIncrementPrice;
    @ApiModelProperty(value = "是否为当前值")
    private Boolean current;
    @ApiModelProperty(value = "创建时间")
    private Date updateTime;
    @ApiModelProperty(value = "创建时间")
    private Date createTime;
    @ApiModelProperty(value = "订单类型")
    private String orderType;

    public void calculate(BigDecimal profit,  BigDecimal commissionPercent, BigDecimal incrementPercent,
                          BigDecimal increment2dPercent, BigDecimal beforeInit, BigDecimal currentInit) { //price cost commissionPercent incrementPercent,increment2dPercent,initToken
        this.commissionPercent = commissionPercent;
        this.incrementPercent = incrementPercent;
        this.increment2dPercent = increment2dPercent;
        this.initToken = currentInit;

        this.profit = profit;
        this.commission = this.profit.multiply(this.commissionPercent.divide(new BigDecimal(100),9,BigDecimal.ROUND_HALF_UP));
        this.increment = this.commission.multiply(this.incrementPercent.divide(new BigDecimal(100),9,BigDecimal.ROUND_HALF_UP));
        this.increment2d = this.profit.multiply(this.increment2dPercent.divide(new BigDecimal(100),9,BigDecimal.ROUND_HALF_UP));
        this.realProfit = this.profit.subtract(this.commission).subtract(this.increment).subtract(this.increment2d);
        this.incrementToken = this.commission.divide(this.beforeIncrementPrice,9,BigDecimal.ROUND_HALF_UP);
        if (currentInit.compareTo(beforeInit) > 0) {
            this.afterIncrementToken = this.beforeIncrementToken.add(this.incrementToken).add(currentInit.subtract(beforeInit));
        } else if (currentInit.compareTo(beforeInit) == 0) {
            this.afterIncrementToken = this.beforeIncrementToken.add(this.incrementToken);
        } else {
            this.afterIncrementToken = this.beforeIncrementToken.add(this.incrementToken).subtract(beforeInit.subtract(currentInit));
        }

        this.deltaIncrementPrice = this.increment.divide(this.afterIncrementToken,9,BigDecimal.ROUND_HALF_UP);
        this.afterIncrementPrice = this.beforeIncrementPrice.add(this.deltaIncrementPrice);
        this.increment2dToken = this.increment2d.divide(this.afterIncrementPrice,9,BigDecimal.ROUND_HALF_UP);
    }

    public void initByPre(BigDecimal price,BigDecimal cost,String orderId,BigDecimal beforeToken, BigDecimal beforePrice) {
        this.price=price;
        this.cost=cost;
        this.orderId=orderId;
        this.beforeIncrementToken = beforeToken;
        this.beforeIncrementPrice = beforePrice;
    }

}